<?php

namespace App\Kuafu\Shop\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Enum\DeviceMerchantEnum;
use App\Kuafu\Order\Enum\OrderCodeEnum;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Service\DeviceService;
use App\Kuafu\Shop\Validate\ShopValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class DeviceController extends BaseController
{


    /**
     * 接收监控人流量数据
     * @param Request $request
     * @return JsonResponse
     * @throws ShopException
     */
    public function monitorData(Request $request): JsonResponse
    {
        $data = $request->post('data');
        $valid = ShopValidate::monitorData($request);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_BANS_HAS_SERIAL_NUMBER, $valid);
        }

        $shopId = $request->header("shopId");
        DeviceService::addMonitorData($shopId, $data);
        return $this->success();
    }

    /**
     * 查看监控人流量数据
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function getMonitorData(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', DeviceMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt = (int)$request->get('showEndAt', 0);
        $compareTimeTab = (int)$request->get('compareTimeTab');
        $compareStartAt = (int)$request->get('compareStartAt', 0);
        $compareEndAt = (int)$request->get('compareEndAt', 0);
        $statisticsType = (int)$request->get('statisticsType', DeviceMerchantEnum::TYPE_TOTAL);

        $validate = ShopValidate::getMonitorData($request);
        if ($validate) {
            return $this->fail(OrderCodeEnum::ERR_ORDER_PARAM_VALID, $validate);
        }

        $shopId = $request->header("shopId");
        $brandId = $request->header("brandId");
        $data = (new DeviceService())
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->setCompareTimeTab($compareTimeTab, $compareStartAt, $compareEndAt)
            ->getMonitorData($shopId, $brandId, $statisticsType);

        return $this->success($data);
    }

}
